package com.example.demo6.ui.edit;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo6.Account;
import com.example.demo6.R;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.SQLite.PickupGoods;
import com.example.demo6.SQLite.PickupGoodsDao;

public class edit_pickup_informationFragment extends Fragment {

    String username;
    private EditText editPickupItem, editPickupPlace, editPickup, editPickupPhone, editPickupTime, editPickupItemDetail;
    private Button LostUpdateBtn,PickupCancelBtn;
    private PickupGoodsDao pickupGoodsDao;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_pickup_information, container, false);
        initViews(view);
        initDatabase();
        editPickup.setText(Account.getInstance().getCurrentUsername());
        LostUpdateBtn.setOnClickListener(v -> {
            // Check if all required fields are filled before proceeding
            if (isInputValid()) {
                String pickupGoods = editPickupItem.getText().toString().trim();
                String pickup = editPickup.getText().toString().trim();
                String pickupPlace = editPickupPlace.getText().toString().trim();
                String pickupTime = editPickupTime.getText().toString().trim();
                String pickupPhone = editPickupPhone.getText().toString().trim();
                String pickupDetail = editPickupItemDetail.getText().toString().trim();

                PickupGoods newItem = new PickupGoods(pickupGoods, pickup, pickupPlace, pickupTime, pickupPhone, pickupDetail);
                new edit_pickup_informationFragment.InsertPickupGoodsTask(getActivity(), pickupGoodsDao).execute(newItem);
            }
        });
        Button PickupCancelBtn = view.findViewById(R.id.PickupCancelBtn);
        PickupCancelBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                navigateTo(R.id.navigation_home);
            }
        });

        return view;
    }

    private boolean isInputValid() {
        if (editPickupItem == null || editPickup == null) {
            Log.e("InputCheck", "One or more input fields are null.");
            return false;
        }

        String pickupGoods = editPickupItem.getText().toString().trim();
        String pickup = editPickup.getText().toString().trim();

        if (TextUtils.isEmpty(pickupGoods) || TextUtils.isEmpty(pickup)) {
            Toast.makeText(getActivity(), "物品名称和拾物主姓名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    // 为了简化导航操作，可以创建一个辅助方法
    private void navigateTo(int destinationId) {
        NavController navController = Navigation.findNavController(requireView());
        Bundle bundle=new Bundle();
        bundle.putString("username", username);//传参
        navController.navigate(destinationId,bundle);
    }

    private void initViews(View view) {
        editPickupItem = view.findViewById(R.id.editPickupItem);
        editPickupPlace = view.findViewById(R.id.editPickupPlace);
        editPickup = view.findViewById(R.id.editPickup);
        editPickupPhone = view.findViewById(R.id.editPickupPhone);
        editPickupTime = view.findViewById(R.id.editPickupTime);
        editPickupItemDetail = view.findViewById(R.id.editPickupItemDetail);
        LostUpdateBtn = view.findViewById(R.id.LostUpdateBtn);
    }

    private void initDatabase() {
        MyDatabase db = MyDatabase.getInstance(getContext());
        pickupGoodsDao = db.pickupGoodsDao();
        if (pickupGoodsDao == null) {
            Log.e("DatabaseInit", "PickupGoodsDao is null.");
        }
    }
    // Async Task for inserting data into the database
    private static class InsertPickupGoodsTask extends AsyncTask<PickupGoods, Void, Void> {
        private final PickupGoodsDao dao;
        private final Activity activity;

        InsertPickupGoodsTask(Activity activity, PickupGoodsDao dao) {
            this.activity = activity;
            this.dao = dao;
        }

        @Override
        protected Void doInBackground(PickupGoods... pickupGoods) {
            PickupGoods item = pickupGoods[0];
            dao.insertPickupGoods(item); // Assuming 'insert' is a method in LostGoodsDao that handles the insertion.
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Toast.makeText(activity, "添加成功", Toast.LENGTH_SHORT).show();
            NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment_activity_main);
            navController.navigate(R.id.pickupFragment);
        }
    }
}